<?php

class Admin_MediaController extends CMS_Controller_Action_Admin {

    public function init() {
        parent::init();
    }

    public function indexAction() {
        $media = new CMS_Models_Media();
        $this->view->images = $media->getAll();
    }

    public function deleteAction() {
        $this->disableView();
        $request = $this->_request->getParams();
        $file = $request['file'];
        $media = new CMS_Models_Media();
        $media = $media->getMediaById($file);
        $media->delete();
        $this->_redirect("/admin/media");
    }

    public function editmediaAction() {
        $form = $this->editMediaForm();
        $this->view->form = $form;

        if (!$this->getRequest()->isPost()) {
            return;
        } else {
            if (!$form->isValid($_POST)) {
                $this->view->form = $form;
                return;
            } else {
                $values = $form->getValues();
                $media = new CMS_Models_Media();
                $media = $media->getMediaById($values['common']['id_file']);

                $media->name = $values['common']['name'];
                $media->description = $values['common']['description'];
                if ($values['common']['image'] == NULL) {
                    $media->file = $values['common']['imageHidden'];
                } else {
                    $upload = $form->common->image->getTransferAdapter();
                    $info = $upload->getFileInfo();
                    if (!$upload->receive()) {
                        $messages = $upload->getMessages();
                        echo implode("\n", $messages);
                        exit;
                    }
                    $media->file = $info['image']['name'];
                }
                $media->save();
                $this->_redirect("/admin/media");
            }
        }
    }

    private function editMediaForm() {
        $form = new Zend_Form();
        $form->setAction("/admin/media/editmedia")
                ->setMethod('post');
        $form->setAttrib("class", "jNice");
        $request = $this->_request->getParams();
        $file = $request['file'];
        $media = new CMS_Models_Media();
        $media = $media->getMediaById($file);
        $common = new Zend_Form_SubForm();
        $name = $common->createElement('text', 'name');
        $name->setAttrib("class", "text-long");
        $name->addValidator('NotEmpty')->setLabel("Titulo")->setRequired(true);
        $name->addErrorMessage($this->view->t->_('Este_campo_es_obligatorio'));
        $name->setValue($media->name);

        $description = $common->createElement('text', 'description');
        $description->setAttrib("class", "text-long");
        $description->setLabel("Description");
        $description->setValue($media->description);

        $image = new Zend_Form_Element_File('image');

        $image->addValidator('Extension', false, array('gif', 'jpg', 'png'))->addErrorMessage('Formato de imagen no valido');
        $image->setDestination(str_replace("\\", "/", dirname(dirname(dirname(dirname(dirname(__FILE__)))))) . '/upload/multimedia/');

        $imageHidden = $common->createElement('hidden', 'imageHidden');
        $imageHidden->setLabel('Imagen');
        $imageHidden->setValue($media->file);

        $this->view->value = $media->file;

        $id = $common->createElement("hidden", 'id_file');
        $id->setValue($media->id);

        $common->addElement($name);
        $common->addElement($description);
        $common->addElement($imageHidden);
        $common->addElement($image);

        $common->addElement($id);

        $form->addSubForms(array("common" => $common));
        $form->addElement('submit', 'add', array('label' => $this->view->t->_('Aceptar')));
        return $form;
    }

    private function addMediaForm() {
        $form = new Zend_Form();
        $form->setAction("/admin/media/newmedia")
                ->setMethod('post');
        $form->setAttrib("class", "jNice");
        $common = new Zend_Form_SubForm();
        $name = $common->createElement('text', 'name');
        $name->setAttrib("class", "text-long");
        $name->addValidator('NotEmpty')->setLabel($this->view->t->_('Titulo'))->setRequired(true);
        $name->addErrorMessage($this->view->t->_('Este_campo_es_obligatorio'));

        $description = $common->createElement('textarea', 'description');
        $description->setLabel("Descripcion");
        $description->setAttrib("class", "text-long");

        $image = new Zend_Form_Element_File('image');
        $image->setLabel('Imagen');
        $image->addValidator('Extension', false, array('gif', 'jpg', 'png'))->addErrorMessage('Formato de imagen no valido');
        $image->setDestination(str_replace("\\", "/", dirname(dirname(dirname(dirname(dirname(__FILE__)))))) . '/upload/multimedia/');

        $common->addElement($name);
        $common->addElement($description);
        $common->addElement($image);
        $form->addSubForms(array("common" => $common));
        $form->addElement('submit', 'add', array('label' => $this->view->t->_('Aceptar')));
        return $form;
    }

    public function newmediaAction() {
        $form = $this->addMediaForm();
        $this->view->form = $form;

        if (!$this->getRequest()->isPost()) {
            return;
        } else {
            if (!$form->isValid($_POST)) {
                $this->view->form = $form;
                return;
            } else {
                $values = $form->getValues();
                $media = new CMS_Models_Media();
                $media = $media->createRow();
                $media->name = $values['common']['name'];
                $media->description = $values['common']['description'];
                if ($values['common']['image'] == NULL) {
                    $media->file = '';
                } else {
                    $upload = $form->common->image->getTransferAdapter();
                    $info = $upload->getFileInfo();
                    if (!$upload->receive()) {
                        $messages = $upload->getMessages();
                        echo implode("\n", $messages);
                        exit;
                    }
                    $media->file = $info['image']['name'];
                }
                $media->save();
                $this->_redirect("/admin/media");
            }
        }
    }

}

?>